﻿using MongdbHelper;
using MongoDB.Driver;
using PerformanceBll.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using UIModel.Mongo;

namespace MongoTest
{
    public class Program
    {
        private static string collection;
        private static int count;
        static void Main(string[] args)
        {

            //var momgo2 = new MongoDBHelper("mongodb://192.168.80.216:30001", "StaffAge1");

            //Console.WriteLine("请选择：1.并发插入 2.单次插入 3.查询,4。非片建查询");
            //int chose = Convert.ToInt32(Console.ReadLine());
            //switch (chose)
            //{
            //    case 1:

            //        Console.WriteLine("请选择递增或者哈希：0-递增,1-哈希");
            //        var size = Convert.ToInt32(Console.ReadLine());
            //        if (size == 0)
            //        {
            //            new SynMongoData().DanciTaskJob(10000);
            //            new SynMongoData().DanciTaskJob(20000);
            //            new SynMongoData().DanciTaskJob(50000);
            //            new SynMongoData().DanciTaskJob(100000);
            //            LoggerHelper.Log("=======================递增并发完成==============================");
            //        }
            //        else if (size == 1)
            //        {
            //            new SynMongoData().DanciTaskJob2(10000);
            //            new SynMongoData().DanciTaskJob2(20000);
            //            new SynMongoData().DanciTaskJob2(50000);
            //            new SynMongoData().DanciTaskJob2(100000);
            //            LoggerHelper.Log("=======================哈希并发完成==============================");
            //        }
            //        break;
            //    case 2:

            //        Console.WriteLine("请选择递增或者哈希：0-递增,1-哈希");
            //        size = Convert.ToInt32(Console.ReadLine());
            //        if (size == 0)
            //        {
            //            new SynMongoData().InsertJob1(10000);
            //            new SynMongoData().InsertJob1(20000);
            //            new SynMongoData().InsertJob1(50000);
            //            new SynMongoData().InsertJob1(100000);
            //            LoggerHelper.Log("=======================递增单次完成==============================");
            //        }
            //        else if (size == 1)
            //        {
            //            new SynMongoData().InsertJob2(10000);
            //            new SynMongoData().InsertJob2(20000);
            //            new SynMongoData().InsertJob2(50000);
            //            new SynMongoData().InsertJob2(100000);
            //            LoggerHelper.Log("=======================哈希单次完成==============================");
            //        }
            //        break;
            //    case 3:

            //        var date = "2017/8/1";

            //        Console.WriteLine("请选择递增或者哈希：0-递增,1-哈希");
            //        size = Convert.ToInt32(Console.ReadLine());
            //        if (size == 0)
            //        {
            //            new SynMongoData().FindJob(10000, Convert.ToDateTime(date));

            //            new SynMongoData().FindJob(20000, Convert.ToDateTime(date));

            //            new SynMongoData().FindJob(50000, Convert.ToDateTime(date));

            //            new SynMongoData().FindJob(500000, Convert.ToDateTime(date));
            //            LoggerHelper.Log("=======================递增完成==============================");
            //        }
            //        else if (size == 1)
            //        {
            //            new SynMongoData().FindJob2(10000, Convert.ToDateTime(date));

            //            new SynMongoData().FindJob2(20000, Convert.ToDateTime(date));

            //            new SynMongoData().FindJob2(50000, Convert.ToDateTime(date));

            //            new SynMongoData().FindJob2(500000, Convert.ToDateTime(date));
            //            LoggerHelper.Log("=======================哈希完成==============================");
            //        }

            //        break;
            //    case 4:
            //        Console.WriteLine("请选择递增或者哈希：0-递增,1-哈希");
            //        size = Convert.ToInt32(Console.ReadLine());
            //        if (size == 0)
            //        {
            //            new SynMongoData().FindJob3(1000);

            //            new SynMongoData().FindJob3(20000);

            //            new SynMongoData().FindJob3(50000);

            //            new SynMongoData().FindJob3(500000);
            //            LoggerHelper.Log("=======================递增完成==============================");
            //        }
            //        else if (size == 1)
            //        {
            //            new SynMongoData().FindJob4(10000);

            //            new SynMongoData().FindJob4(20000);

            //            new SynMongoData().FindJob4(50000);

            //            new SynMongoData().FindJob4(500000);
            //            LoggerHelper.Log("=======================哈希完成==============================");
            //        }

            //        break;
            //}

  
            //递增单次
            dd(@"G:\rizhi\递增单次\MongoDB_config_2019-01-18_18-31-14.log", "dzsingle_config");
            dd(@"G:\rizhi\递增单次\MongoDB_mongos_2019-01-18_18-31-18.log", "dzsingle_mongos");
            dd(@"G:\rizhi\递增单次\MongoDB_rbsA_1 (分片)_2019-01-18_18-31-21.log", "dzsingle_a");
            dd(@"G:\rizhi\递增单次\MongoDB_rbsB_1(分片)_2019-01-18_18-31-24.log", "dzsingle_b");

            dd(@"G:\rizhi\哈希并发\MongoDB_config_2019-01-18_18-33-56.log", "hxconcurrent_config");
            dd(@"G:\rizhi\哈希并发\MongoDB_mongos_2019-01-18_18-33-58.log", "hxconcurrent_mongos");
            dd(@"G:\rizhi\哈希并发\MongoDB_rbsA_1 (分片)_2019-01-18_18-34-00.log", "hxconcurrent_a");
            dd(@"G:\rizhi\哈希并发\MongoDB_rbsB_1(分片)_2019-01-18_18-34-02.log", "hxconcurrent_b");
  
            dd(@"G:\rizhi\哈希单次\MongoDB_config_2019-01-18_18-37-06.log", "hxsingle_config");
            dd(@"G:\rizhi\哈希单次\MongoDB_mongos_2019-01-18_18-37-08.log", "hxsingle_mongos");
            dd(@"G:\rizhi\哈希单次\MongoDB_rbsA_1 (分片)_2019-01-18_18-37-09.log", "hxsingle_a");
            dd(@"G:\rizhi\哈希单次\MongoDB_rbsB_1(分片)_2019-01-18_18-37-11.log", "hxsingle_b");



            dd(@"G:\rizhi\递增查询非片键\MongoDB_config_2019-01-18_19-01-53.log", "dzquerynotpianjian_config");
            dd(@"G:\rizhi\递增查询非片键\MongoDB_mongos_2019-01-18_19-01-55.log", "dzquerynotpianjian_mongos");
            dd(@"G:\rizhi\递增查询非片键\MongoDB_rbsA_1 (分片)_2019-01-18_19-01-56.log", "dzquerynotpianjian_a");
            dd(@"G:\rizhi\递增查询非片键\MongoDB_rbsB_1(分片)_2019-01-18_19-01-58.log", "dzquerynotpianjian_b");

            dd(@"G:\rizhi\递增查询片键\MongoDB_config_2019-01-18_18-59-20.log", "dzquerypianjian_config");
            dd(@"G:\rizhi\递增查询片键\MongoDB_mongos_2019-01-18_18-59-22.log", "dzquerypianjian_mongos");
            dd(@"G:\rizhi\递增查询片键\MongoDB_rbsA_1 (分片)_2019-01-18_18-59-30.log", "dzquerypianjian_a");
            dd(@"G:\rizhi\递增查询片键\MongoDB_rbsB_1(分片)_2019-01-18_18-59-31.log", "dzquerypianjian_b");

            dd(@"G:\rizhi\哈希查询非片键\MongoDB_config_2019-01-18_19-04-37.log", "hxquerynotpianjian_config");
            dd(@"G:\rizhi\哈希查询非片键\MongoDB_mongos_2019-01-18_19-04-40.log", "hxquerynotpianjian_mongos");
            dd(@"G:\rizhi\哈希查询非片键\MongoDB_rbsA_1 (分片)_2019-01-18_19-04-41.log", "hxquerynotpianjian_a");
            dd(@"G:\rizhi\哈希查询非片键\MongoDB_rbsB_1(分片)_2019-01-18_19-04-42.log", "hxquerynotpianjian_b");

            dd(@"G:\rizhi\哈希查询片键\MongoDB_config_2019-01-18_19-03-38.log", "hxpianjian_config");
            dd(@"G:\rizhi\哈希查询片键\MongoDB_mongos_2019-01-18_19-03-40.log", "hxpianjian_mongos");
            dd(@"G:\rizhi\哈希查询片键\MongoDB_rbsA_1 (分片)_2019-01-18_19-03-41.log", "hxpianjian_a");
            dd(@"G:\rizhi\哈希查询片键\MongoDB_rbsB_1(分片)_2019-01-18_19-03-42.log", "hxpianjian_b");




            Console.WriteLine("处理完成");
            Console.ReadLine();

        }


        public static void test2(int i)
        {
            var momgo = new MongoDBHelper("BaseGuest");
            Console.WriteLine($"线程{i}启动");
            try
            {
                Stopwatch sbin = new Stopwatch();
                sbin.Start();
                Console.WriteLine($"未分片数据获取中.......");
                var querylist = momgo.GetAll<BaseGuest>(x => x.GstID >= 2800000, x => x.GstID).ToList();
                sbin.Stop();
                Console.WriteLine($"未分片数据获取结束,获取数据：{querylist.Count},耗时：{sbin.ElapsedMilliseconds}.......");
                Console.WriteLine("==================分割线==================");
                Console.WriteLine("执行完成");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

            }

        }

        public static void dd(string path, string connection)
        {
            var clist = new List<Chartmodel>();
            using (
              var filestream = new System.IO.FileStream(@path, System.IO.FileMode.Open,
                  System.IO.FileAccess.Read, FileShare.Read))
            {
                using (var sw = new StreamReader(filestream))
                {
                    var str = sw.ReadToEnd();
                    var list = Regex.Matches(str, @"Cpu.*d");
                    foreach (Match item in list)
                    {
                        str = item.Value.Replace("Cpu(s):", "").Replace(",", "").Trim();
                        Regex reg = new Regex(@"\s+");
                        string modified = reg.Replace(str, ",");
                        var list2 = modified.Split(',');
                        clist.Add(new Chartmodel() { key = DateTime.Now.ToString("HH:mm:ss"), value = Convert.ToDecimal(list2[0].Replace("%us", "").ToString()) });

                    }
                }
            }
            var momgo = new MongoDBHelper("mongodb://admin:adminlinux@52.83.104.122:30000,52.83.216.230:30000/", "Chart", connection);//旧数据库读取
            momgo.InsertMany(clist);
        }
    }
    public class Staff
    {
        public MongoDB.Bson.ObjectId _id = MongoDB.Bson.ObjectId.GenerateNewId();
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Bir { get; set; }
    }

}
